//
//  LoginViewController.swift
//  777Proj
//
//  Created by 陈宇 on 2017/5/15.
//  Copyright © 2017年 🐜. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class LoginViewController: BaseViewController {
    
    @IBOutlet weak var tf_username: UITextField!
    @IBOutlet weak var tf_password: UITextField!
    @IBOutlet weak var btn_login: UIButton!
    @IBOutlet weak var btn_forget: UIButton!
    
    typealias completeBlock = () -> Void
    var completeBlock: completeBlock?
    
    convenience init(complete: @escaping completeBlock) {
        self.init()
        self.completeBlock = complete
    }
    
    override func configureLeftItem() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "common.back"), style: .plain, target: nil, action: nil)
        let _ = self.navigationItem.leftBarButtonItem?.rx.tap.throttle(0.1, scheduler: MainScheduler.instance).takeUntil(self.rx.deallocated).subscribe(onNext: {
            self.dismiss(animated: true, completion: nil)
        })
    }
    
    override func configureUI() {
        super.configureUI()
        
        title = "登录"
    }
    
    override func configureEvent() {
        super.configureEvent()
        
        btn_login.rx.tap.bind {
            api.request(.Login(username: "ryan", password: "111111")).unWrapper().subscribe(onNext: { (json) in
                guard let token = json as? String else {
                    return
                }
                
                UIApplication.token = token
                if UIApplication.isLogin {
                    self.completeBlock?()
                    self.dismiss(animated: true, completion: nil)
                }
            }).addDisposableTo(self.disposeBag)
        }.addDisposableTo(disposeBag)
    }
    
}
